package com.xiong.miaosha.service;

import com.xiong.miaosha.controller.MiaoshaController;
import com.xiong.miaosha.dao.GoodsDao;
import com.xiong.miaosha.dao.MiaoshaUserDao;
import com.xiong.miaosha.domain.Goods;
import com.xiong.miaosha.domain.MiaoshaGoods;
import com.xiong.miaosha.domain.MiaoshaUser;
import com.xiong.miaosha.exception.GlobalException;
import com.xiong.miaosha.redis.MiaoshaUserKey;
import com.xiong.miaosha.redis.RedisService;
import com.xiong.miaosha.result.CodeMsg;
import com.xiong.miaosha.utils.MD5Util;
import com.xiong.miaosha.utils.UUIDUtil;
import com.xiong.miaosha.vo.GoodsVo;
import com.xiong.miaosha.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author wardseptember
 * @create 2020-03-16 11:22
 */
@Service
public class GoodsService {
    private static Logger log = LoggerFactory.getLogger(GoodsService.class);


    @Autowired
    GoodsDao goodsDao;

    public List<GoodsVo> listGoodsVo() {
        return goodsDao.listGoodsVo();
    }

    public GoodsVo getGoodsVoByGoodsId(long goodsId) {
        return goodsDao.getGoodsVoByGoodsId(goodsId);
    }

    public boolean reduceStock(GoodsVo goods) {
        MiaoshaGoods g = new MiaoshaGoods();
        g.setGoodsId(goods.getId());
        int ret = goodsDao.reduceStock(g);
        log.info("reduceStock" + ret);
        log.info("goodsId" + goods.getId());
        return ret > 0;
    }

    /**
     * 重置库存
     * @param goodsList
     */
    public void resetStock(List<GoodsVo> goodsList) {
        for(GoodsVo goods : goodsList ) {
            MiaoshaGoods g = new MiaoshaGoods();
            g.setGoodsId(goods.getId());
            g.setStockCount(goods.getStockCount());
            goodsDao.resetStock(g);
        }
    }
}
